﻿using System;
using System.Windows.Data;
using System.Windows.Controls;

namespace BizControl.Presentation.StickyNotesModule.Converters
{
  public class IndexToAngleConverter : IValueConverter
  {
    private const int MinAngle = 3;
    private const int MaxAngle = 10;

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      double angleToReturn = new Random(DateTime.Now.Millisecond).Next(MinAngle, MaxAngle);
      ListBoxItem item = (ListBoxItem)value;
      ListBox listBox = (ListBox)ItemsControl.ItemsControlFromItemContainer(item);
      int itemIndex = listBox.ItemContainerGenerator.IndexFromContainer(item);
      if (itemIndex % 2 != 0)
        angleToReturn = angleToReturn * -1;
      return angleToReturn;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      return null;
    }

    #endregion
  }
}
